package priv.dk.springboot.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import priv.dk.springboot.exception.BusinessException;

import static priv.dk.springboot.util.CommonUtil.NO_LOGIN;

@Component
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object employee = session.getAttribute("employee");
        // 未登录处理
        if (employee == null) throw new BusinessException(NO_LOGIN, "未登录");
        // 放行
        return true;
    }
}
